package cn.hftoday.org;

import java.util.*;
import org.apache.log4j.*;
import cn.hftoday.db.*;
import cn.hftoday.core.*;
import cn.hftoday.util.*;

/**
 * organization
 * @author lane.cn@gmail.com
 *
 */
public class Organization extends PartyBase {
	private static final Logger LOG = Logger.getLogger(Organization.class);
	
	private String organizationTypeId;
	private String alias;
	private String location;
	private String companyId;
	
	public OrganizationType getOrganizationType() {
		return (OrganizationType)Mapper.getInstance().queryForObject("SelectOrganizationType", organizationTypeId);
	}
	
	public void setOrganizationType(OrganizationType organizationType) {
		if (organizationType == null) {
			organizationTypeId = null;
		}
		else {
			organizationTypeId = organizationType.getId();
		}
	}
	
	public String getAlias() {
		return alias;
	}
	
	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getLocation() {
		return location;
	}
	
	public void setLocation(String location) {
		this.location = location;
	}
	
	public Company getCompany() {
		return (Company)Mapper.getInstance().queryForObject("SelectCompanyById", companyId);
	}
	
	public void setCompany(Company company) {
		if (company == null) {
			companyId = null;
		}
		else {
			companyId = company.getId();
		}
	}
	
	public String getIcon() {
		return getOrganizationType().getIcon();
	}
	
	public String getPartyType() {
		return "organization";
	}
	
	private static void _select(Object object, RecordSet rs, int i) {
		Organization o = (Organization)object;
		o.id = rs.getItem(i, "f_id");
		o.name = rs.getItem(i, "f_name");
		o.hidden = rs.getItem(i, "f_hidden").equals("1");
		o.createTime = CalendarParser.toCalendar(rs.getItem(i, "f_createtime"));
		o.updateTime = CalendarParser.toCalendar(rs.getItem(i, "f_updatetime"));
		o.organizationTypeId = rs.getItem(i, "f_orgtype");
		o.alias = rs.getItem(i, "f_alias");
		o.location = rs.getItem(i, "f_location");
		o.companyId = rs.getItem(i, "f_company");
	}
	
	private static String _insert(Object object, String sql) {
		Organization o = (Organization)object;
		return StringParser.formatSql(sql,
			o.id,
			o.name,
			o.hidden ? "1" : "0",
			CalendarParser.toString(o.createTime),
			CalendarParser.toString(o.updateTime),
			o.organizationTypeId,
			o.alias,
			o.location,
			o.companyId
		);
	}
	
	private static String _update(Object object, String sql) {
		Organization o = (Organization)object;
		return StringParser.formatSql(sql,
			o.id,
			o.name,
			o.hidden ? "1" : "0",
			CalendarParser.toString(o.createTime),
			CalendarParser.toString(o.updateTime),
			o.organizationTypeId,
			o.alias,
			o.location,
			o.companyId
		);
	}
	
	private static String _delete(Object object, String sql) {
		Organization o = (Organization)object;
		return StringParser.formatSql(sql, o.id);
	}
}
